package client;

import general.AuctionOptions;
import general.AuctioneerClientInterface;
import general.Bid;
import general.MarketeerClientInterface;
import general.Product;
import general.ProductRecord;

import java.rmi.RemoteException;
import java.util.LinkedList;

import net.jini.core.lookup.ServiceItem;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.discovery.LookupDiscoveryManager;
import net.jini.lease.LeaseRenewalManager;
import net.jini.lookup.ServiceDiscoveryManager;

public class Client implements AuctioneerClientInterface{
	
	private MarketeerClientInterface fMarketeerProxy;
	
	public Client() {
		
		try {		
			// Get lookup service
			LookupDiscoveryManager discoveryMgr = new LookupDiscoveryManager(new String [] {"JORSEBREGGIE"}, null, null);
			ServiceDiscoveryManager serviceMgr = new ServiceDiscoveryManager(discoveryMgr, new LeaseRenewalManager());

			// Search for MarketeteerClient service
			Class[] classes = new Class[] { MarketeerClientInterface.class }; 
			ServiceTemplate template = new ServiceTemplate(null, classes, null);
			ServiceItem item = null;
			item = serviceMgr.lookup(template, null, 1000L);

			// Get the service
			if (item == null) {
				System.out.println("MarketeerClient service not found"); 
				System.exit(1);
			}
			fMarketeerProxy = (MarketeerClientInterface) item.service;
			if (fMarketeerProxy == null) { 
				System.out.println("MarketeerClient service not found"); 
				System.exit(1);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("Client started...");
	}
	
	public void addProduct(Product product, AuctionOptions auctionOptions) {
		try {
			fMarketeerProxy.addProduct(product, auctionOptions);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
	
	public LinkedList<ProductRecord> searchProducts(String name) {
		try {
			return fMarketeerProxy.searchProducts(name);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return null;
	}

	// This function will be called if a new client has joined the auction for the vikrey auction model
	public void notifyJoin(String name) throws RemoteException {
		
	}

	// This function will inform the bidders if the auction is finished and wich product they won or lost.
	public void notifyBidders(boolean win, Product p) throws RemoteException {
		
	}

	// This function will be called if there is a new highest bid (English Action) or new Lowest bid (Dutch Auction) 
	public void updateBidders(Bid current) throws RemoteException {
		
	}
}
